<template>
  <el-card shadow="never">
    <vxe-toolbar :custom="{immediate:true}">
      <template v-slot:buttons>
        <el-button v-if="$store.state.user.superAdmin === 1" type="primary" @click="addOrUpdateHandle()">
          {{ $t('add') }}</el-button>
        <el-button @click="$refs.xTree.setAllTreeExpansion(true)">{{ $t('openall') }}</el-button>
        <el-button @click="$refs.xTree.clearTreeExpand()">{{ $t('closeall') }}</el-button>
      </template>
      <template v-slot:tools>
        <el-button icon="el-icon-refresh" @click="getTreeDataList()" circle></el-button>
        <el-button icon="el-icon-download" @click="$refs.xTable.openExport()" circle></el-button>
      </template>
    </vxe-toolbar>
    <vxe-table ref="xTree" :loading="dataListLoading" auto-resize :tree-config="{children: 'children',trigger:'cell'}"
      :data="dataList">
      <vxe-table-column field="dictValue" title="dict.dictValue" tree-node></vxe-table-column>
      <vxe-table-column field="dictLabel" title="dict.label"></vxe-table-column>
      <vxe-table-column field="dictType" title="dict.dictName"></vxe-table-column>
      <vxe-table-column field="sort" title="dict.sort" width="60" align="center"></vxe-table-column>
      <vxe-table-column field="remark" title="dict.remark"></vxe-table-column>
      <vxe-table-column field="status" title="dict.state" width="80" align="center">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.status == 0" size="small" type="danger">{{ $t('dict.status0') }}</el-tag>
          <el-tag v-else size="small" type="success">{{ $t('dict.status1') }}</el-tag>
        </template>
      </vxe-table-column>
      <vxe-table-column v-if="$store.state.user.superAdmin === 1" title="handle" width="180">
        <template slot-scope="scope" align="center">
          <el-button type="text" size="small" icon="el-icon-plus" @click="addChild(scope.row)">{{
            $t('addChild') }}</el-button>
          <el-button type="text" icon="el-icon-edit" size="small" @click="addOrUpdateHandle(scope.row)">{{
              $t('update') }}
          </el-button>
          <el-button type="text" icon="el-icon-delete" size="small" @click="deleteHandle(scope.row.id)">{{
              $t('delete') }}
          </el-button>
        </template>
      </vxe-table-column>
    </vxe-table>
    <!-- 弹窗, 新增 / 修改 -->
    <add-or-update v-if="addOrUpdateVisible" ref="addOrUpdate" @refreshDataList="getTreeDataList"></add-or-update>
  </el-card>
</template>
<script>
import mixinViewModule from '@/mixins/view-module'
import AddOrUpdate from './dictdata-tree-add-or-update'
import { treeDataTranslate } from '@/utils/index'
export default {
  mixins: [mixinViewModule],
  data () {
    return {
      searchShow: true,
      mixinViewModuleOptions: {
        getDataListURL: '/sys/sysdictdata/list',
        getDataListIsPage: false,
        deleteURL: '/sys/sysdictdata',
        deleteIsBatch: true,
        exportURL: '/sys/sysdictdata/export',
        getTreeDataListURL: '/sys/sysdictdata/list',
        treeType: true
      },
      dataForm: {
        pnames: '',
        status: '',
        dictType: this.$route.params.dictType.dictType
      },
      dictType: {}
    }
  },
  components: {
    AddOrUpdate
  },
  activated () {
    // 通过路由参数pid, 控制列表请求操作
    if (this.$route.params.dictType) {
      this.dictType = this.$route.params.dictType
      this.dataForm.dictType = this.dictType.dictType
      this.getTreeDataList()
    } else {
      console.log('路由调转')
    }
  },
  methods: {
    // 新增 / 修改
    addOrUpdateHandle (row) {
      let id
      if (row) {
        id = row.id
      }
      this.addOrUpdateVisible = true
      this.getTreeCompList(id).then(res => {
        this.$refs.addOrUpdate.parentTreeData = res
        this.$nextTick(() => {
          this.$refs.addOrUpdate.dataForm.id = id
          this.$refs.addOrUpdate.dataForm.dictType = this.dictType.dictType
          this.$refs.addOrUpdate.dataForm.dictTypeName = this.dictType.dictName
          this.$refs.addOrUpdate.dictType = this.dictType
          this.$refs.addOrUpdate.init()
        })
      })
    },
    addChild (row) {
      this.addOrUpdateVisible = true
      this.getTreeCompList().then(res => {
        this.$refs.addOrUpdate.parentTreeData = res
        this.$nextTick(() => {
          this.$refs.addOrUpdate.dataForm.id = null
          this.$refs.addOrUpdate.dataForm.pid = row.id
          this.$refs.addOrUpdate.dataForm.dictType = this.dictType.dictType
          this.$refs.addOrUpdate.dataForm.dictTypeName = this.dictType.dictName
          this.$refs.addOrUpdate.dictType = this.dictType
          this.$refs.addOrUpdate.init()
        })
      })
    },
    // 父级树组件
    getTreeCompList (id) {
      return new Promise(resolve => {
        this.$http.get(
          this.mixinViewModuleOptions.getTreeDataListURL,
          {
            params: {
              id_ne: id,
              pid_ne: id,
              dictType: this.dictType.dictType
            }
          }
        ).then(({ data: res }) => {
          if (res.code !== 0) {
            return false
          }
          resolve(treeDataTranslate(res.data, 'id', 'pid'))
        }).catch(err => {
          return this.$message.error(err)
        })
      })
    }
  }
}
</script>
